package cn.group7.padserver.service.impl;

import cn.group7.padserver.dao.DiscussDao;
import cn.group7.padserver.dao.KnowledgeDao;
import cn.group7.padserver.entity.Page;
import cn.group7.padserver.pojo.Discuss;
import cn.group7.padserver.pojo.Knowledge;
import cn.group7.padserver.service.KnowledgeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 谷汉斌
 * @description 知识服务实现类
 * @createTime 2024/8/2 下午9:29
 */
@Service
public class KnowledgeServiceImpl implements KnowledgeService {

    @Resource
    private KnowledgeDao knowledgeDao;

    @Resource
    private DiscussDao discussDao;

    /**
     * 分页查询
     *
     * @param pageNum  页码
     * @param pageSize 每页显示的条数
     * @return 分页信息
     */
    @Override
    public Page<Knowledge> findPage(Integer pageNum, Integer pageSize) {
        // 页码小于等于1，则默认为1
        if (pageNum <= 1) {
            pageNum = 1;
        }
        //开始页码-1*每页显示的条数
        int start = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>(1);
        map.put("start", start);
        map.put("pageSize", pageSize);
        Page<Knowledge> page = new Page<>();
        page.setData(knowledgeDao.queryKnowledgeByPage(map));
        page.setPageNum(pageNum);
        page.setTotal(knowledgeDao.selectTotal(map));
        page.setPageSize(pageSize);
        return page;
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return 知识信息
     */
    @Override
    public Knowledge selectById(Integer id) {
        return knowledgeDao.selectKnowledgeById(id);
    }

    /**
     * 根据关键字查询
     *
     * @param keys     关键字
     * @param pageNum  页码
     * @param pageSize 每页显示的条数
     * @return 分页信息
     */
    @Override
    public Page<Knowledge> findPageByKeys(String keys, Integer pageNum, Integer pageSize) {
        // 页码小于等于1，则默认为1
        if (pageNum <= 1) {
            pageNum = 1;
        }
        //开始页码-1*每页显示的条数
        int start = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>(1);
        map.put("keys", keys);
        map.put("start", start);
        map.put("pageSize", pageSize);
        Page<Knowledge> page = new Page<>();
        page.setData(knowledgeDao.selectByKeys(map));
        page.setPageNum(pageNum);
        page.setTotal(knowledgeDao.selectTotal(map));
        page.setPageSize(pageSize);
        return page;
    }

    /**
     * 根据id查询
     *
     * @param knowledgeId 知识id
     * @return 知识信息
     */
    @Override
    public List<Discuss> selectDiscuss(Integer knowledgeId) {
        return discussDao.selectDiscussByKnowledgeId(knowledgeId);
    }

    /**
     * 添加讨论信息
     *
     * @param discuss 讨论信息
     * @return 是否添加成功
     */
    @Override
    public boolean addDiscuss(Discuss discuss) {
        return discussDao.addDiscuss(discuss) == 1;
    }

    /**
     * 根据用户名查询
     *
     * @param name 用户名
     * @return 知识信息
     */
    @Override
    public List<Knowledge> selectByUser(String name) {
        return knowledgeDao.selectByUsername(name);
    }

    /**
     * 添加知识
     *
     * @param knowledge 知识信息
     * @return 是否添加成功
     */
    @Override
    public boolean addKnowledge(Knowledge knowledge) {
        knowledge.setCreateTime(new Date());
        knowledge.setUpdateTime(new Date());
        return knowledgeDao.addKnowledge(knowledge) == 1;
    }

    /**
     * 修改知识
     *
     * @param knowledge 知识信息
     * @return 是否修改成功
     */
    @Override
    public boolean updateKnowledge(Knowledge knowledge) {
        knowledge.setUpdateTime(new Date());
        return knowledgeDao.updateKnowledge(knowledge) == 1;
    }

    /**
     * 删除知识
     *
     * @param knowledgeId 知识id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteKnowledge(Integer knowledgeId) {
        return knowledgeDao.deleteKnowledge(knowledgeId) == 1;
    }
}
